package cn.wolfcode.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.qo.NoticeQuery;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequiresPermissions(value = {"notice:noticeList","公告通知列表页面"},logical = Logical.OR)
    @RequestMapping("/noticeList")
    public String list(Model model, @ModelAttribute("qo") NoticeQuery qo){
        //如果登录的员工是管理员或者经理(admin角色或者Market_Manager角色),就可以看所有的客户,
        // 如果只是普通的销售人员,只能看自己的客户,不能看到别人的
        /*Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee)subject.getPrincipal();
        qo.setSellerId(employee.getId());//当前登录用户的id*/

        //通过id查询中间表得到已读的集合,
        List<Long> ids = noticeService.isReadByEmployeeId(UserContext.getCurrentUser().getId());

        //得到公告集合
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        //遍历集合,想要得到单个的id
        List<Notice> notices = pageInfo.getList();
        for (Notice notice : notices) {
            Long id = notice.getId();
            if(ids.contains(id)){
                notice.setRead(true);
            }
        }
        model.addAttribute("pageInfo", pageInfo);
        return "notice/noticeList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"notice:delete","公告通知列表删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"notice:saveOrUpdate","公告通知列表新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
        }else {
            noticeService.save(notice);
        }
        return new JsonResult();
    }

    @RequestMapping("/show")
    @RequiresPermissions(value = {"notice:show","公告查看内容页面"},logical = Logical.OR)
    public String show(Model model,Long id,boolean read){
        //通过id查询当前查看的公告,并回显
        //如果read是假的,才进行添加
        //通过id获取公告
        Notice notice = noticeService.get(id);
        if(!read){
            //添加中间表
            noticeService.insertRelation(id,UserContext.getCurrentUser().getId());
        }
        //回显
        model.addAttribute("notice",notice);
        return "notice/show";
    }
}
